<?php
namespace Admin\Controller;

class GoodsController extends CommonController
{

    /**
     * 列表
     *
     * @param  void
     * @return void
     */
    public function index()
    {
        if (IS_POST) {
            $this->delete();
        } else {
            $get = I('get.');
            $where = array();
            if (! empty($get['keyword'])) {
                $where['goods_name|goods_desc'] = array( 'like', "%{$get['keyword']}%" );
            }
            if (! empty($get['goods_sn'])) {
                $where['goods_sn'] = $get['goods_sn'];
            }
            if (! empty($get['store_id'])) {
                $where['store_id'] = $get['store_id'];
            }
            $where['status'] = array( 'gt', 0 );
            $list = $this->lists(M('Goods'), true, $where, 'goods_id DESC', 10);
            $store = getStoreList();
            $this->assign('list', $list);
            $this->assign('get', $get);
            $this->assign('store', $store);
            $this->display();
        }
    }
    
    /**
     * 新增
     *
     * @param  void
     * @return void
     */
    public function add()
    {
        if (IS_POST) {
            $data = I('post.');
            if (! empty($_FILES['goods_image']['name'])) {
                $file = D('File');
                $info = $file->upload($_FILES, C('PICTURE_UPLOAD'));
                if (! $info) {
                    $this->error($file->getError());
                }
                if (! empty($info['goods_image'])) {
                    $data['goods_image'] = $info['goods_image']['url'];
                    $data['goods_thumb'] = $info['goods_image']['url'];
                }
            }
            
            $goods = D('Goods');
            $result = $goods->update($data);
            if (false !== $result) {
                $this->success('保存成功', U('index'));
            } else {
                $error = $goods->getError();
                $this->error(empty($error) ? '未知错误' : $error);
            }
        } else {
            $store = getStoreList(false);
            $this->assign('store', $store);
            $this->display();
        }
    }

    /**
     * 修改
     *
     * @param void
     * @return void
     */
    public function edit()
    {
        if (IS_POST) {
            $data = I('post.');
            if (! empty($_FILES['goods_image']['name'])) {
                $file = D('File');
                $info = $file->upload($_FILES, C('PICTURE_UPLOAD'));
                if (! $info) {
                    $this->error($file->getError());
                }
                if (! empty($info['goods_image'])) {
                    $data['goods_image'] = $info['goods_image']['url'];
                    $data['goods_thumb'] = $info['goods_image']['url'];
                }
            }
            
            $goods = D('Goods');
            $result = $goods->update($data);
            if (false !== $result) {
                $this->success('保存成功', U('index'));
            } else {
                $error = $goods->getError();
                $this->error(empty($error) ? '未知错误' : $error);
            }
        } else {
            $id = I('get.id');
            if (empty($id)) {
                $this->error('无效的数据');
            }
            $info = M('Goods')->where('goods_id = ' . $id)->find();
            $store = getStoreList();
            $this->assign('store', $store);
            $this->assign('info', $info);
            $this->display();
        }
    }

    /**
     * 删除
     *
     * @param void
     * @return void
     */
    public function delete()
    {
        $id = array_unique((array) I('id', 0));
        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }
        $where = array( 'goods_id' => array( 'in', $id ) );
        $data = array( 'status' => 0, 'update_time' => time() );
        $result = M('Goods')->where($where)->save($data);
        if ($result) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    } 
    
    /**
     * ajax
     *
     * @param void
     * @return void
     */
    public function ajax()
    {
        $get = I('get.');
        if (! isset($get['branch']) || ! isset($get['id']) || ! isset($get['column']) || ! isset($get['value'])) {
            exit('false');
        }
        if ($get['branch'] == 'state'){
            $data = array( $get['column'] => $get['value'], 'update_time' => time() );
            $result = M('Goods')->where(array('goods_id' => $get['id'] ))->save($data);
            exit($result ? 'true' : 'false');
    
        } else {
            exit('false');
        }
    
    }

    public function getGoodsList()
    {
        $storeID = I('get.store_id');
        if ($storeID){
            $where['store_id'] = $storeID;
        }
        $where['status'] = 1;
        $list = M('Goods')->field('goods_id,goods_name')->where($where)->order('goods_id DESC')->select();
        exit(json_encode($list));
    }
    
}